package com.android.videotest;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.videotest.activity.BannerActivity;
import com.android.videotest.activity.DialogActivity;
import com.android.videotest.activity.FilesActivity;
import com.android.videotest.activity.IjkPlayerActivity;
import com.android.videotest.activity.JiaoziPlayerActivity;
import com.android.videotest.activity.MatrixTestActivity;
import com.android.videotest.activity.RunnableActivity;
import com.android.videotest.activity.SimpleSurfaceVideoActivity;
import com.android.videotest.activity.SimpleVideoActivity;
import com.android.videotest.activity.TextureViewActivity;
import com.android.videotest.activity.TranslateActivity;
import com.android.videotest.activity.VideoViewActivity;
import com.android.videotest.activity.VideoViewActivity2;
import com.android.videotest.activity.ijkDemoActivity;
import com.android.videotest.music.MusicLibrary;
import com.android.videotest.music.ui.MediaPlayerActivity;
import com.android.videotest.music.ui.SimpleMusicActivity;
import com.android.videotest.music.ui.musiclist.MusicListActivity;
import com.android.videotest.video.VideoActivity;
import com.android.videotest.video.listvideo.VideoListActivity;
//import com.sansecy.video.PlayerActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：VideoTest
 * 类 名 称：ListActivity2
 * 创建时间：2020/7/6 12:29 PM
 * 创 建 人：sansecy
 */
public class ListActivity2 extends AppCompatActivity {
    private static final String TAG = "ListActivity-App";
    private Activity mListActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        mListActivity = this;

        if (ContextCompat.checkSelfPermission(mListActivity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(mListActivity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            Log.d(TAG, "onCreate: 请求权限");
            Log.d(this.getClass().getSimpleName() + "-App", "onCreate:请求权限 ");
        } else {
            new Thread(() -> {
                MusicLibrary.initSongList(this);
            }).start();
            Log.d(TAG, "onCreate: 有权限");
            Log.d(this.getClass().getSimpleName() + "-App", "onCreate:有权限 ");
        }

        ArrayList<Pair<String, View.OnClickListener>> strings = new ArrayList<>();
        strings.add(new Pair<>("FilesActivity", v -> FilesActivity.start(mListActivity)));
        strings.add(new Pair<>("VideoViewActivity2", v -> {
//            Uri uri = Uri.parse("android.resource://com.android.videotest/" + R.raw.test_vide);
//            VideoViewActivity2.start(mListActivity, uri);
        }));
        strings.add(new Pair<>("MatrixTestActivity", v -> MatrixTestActivity.start(mListActivity)));
        strings.add(new Pair<>("TranslateActivity", v -> TranslateActivity.start(mListActivity)));
        strings.add(new Pair<>("BannerActivity", v -> BannerActivity.start(mListActivity)));
        strings.add(new Pair<>("RunnableActivity", v -> RunnableActivity.start(mListActivity)));
        strings.add(new Pair<>("JiaoziPlayerActivity", v -> JiaoziPlayerActivity.start(mListActivity)));
        strings.add(new Pair<>("系统控件", null));
        strings.add(new Pair<>("VideoViewActivity", v -> VideoViewActivity.start(mListActivity)));
        strings.add(new Pair<>("SimpleVideoActivity", v -> SimpleVideoActivity.start(mListActivity)));
        strings.add(new Pair<>("SimpleSurfaceVideoActivity", v -> SimpleSurfaceVideoActivity.start(mListActivity)));
        strings.add(new Pair<>("TextureViewActivity", v -> TextureViewActivity.start(mListActivity)));
        strings.add(new Pair<>("IjkPlayerActivity", v -> IjkPlayerActivity.start(mListActivity, "IjkMediaPlayer")));
        strings.add(new Pair<>("ijkDemoActivity", v -> ijkDemoActivity.start(mListActivity)));
        strings.add(new Pair<>("DialogActivity", v -> DialogActivity.start(mListActivity)));
        strings.add(new Pair<>("MediaPlayerActivity", v -> MediaPlayerActivity.start(mListActivity)));
        strings.add(new Pair<>("MusicListActivity", v -> MusicListActivity.start(mListActivity)));
        strings.add(new Pair<>("SimpleMusicActivity", v -> SimpleMusicActivity.start(mListActivity)));
        strings.add(new Pair<>("VideoActivity", v -> VideoActivity.start(mListActivity, "")));
        strings.add(new Pair<>("VideoListActivity", v -> VideoListActivity.start(mListActivity)));
//        strings.add(new Pair<>("EXOPlayerActivity", v -> PlayerActivity.start(mListActivity, Urls.videoUrl2)));

        RecyclerView listView = findViewById(R.id.listView);
        listView.setLayoutManager(new LinearLayoutManager(this));

        MMyAdapter adapter = new MMyAdapter();
        listView.setAdapter(adapter);
        adapter.setPairs(strings);

    }

    private class MMyAdapter extends RecyclerView.Adapter<MyHolder> {
        private List<Pair<String, View.OnClickListener>> mPairs;

        public void setPairs(List<Pair<String, View.OnClickListener>> pairs) {
            mPairs = pairs;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            RecyclerView.LayoutParams marginLayoutParams = new RecyclerView.LayoutParams(-1, -2);
//            marginLayoutParams.setMargins(100, 100, 100, 100);
            TextView convertView = new TextView(mListActivity);
            convertView.setBackgroundResource(R.drawable.card_selector_rectangle);

            convertView.setLayoutParams(marginLayoutParams);
            return new MyHolder(convertView);
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder holder, int position) {
            Pair pair = (Pair) getItem(position);
            ((TextView) holder.itemView).setText((String) pair.first);
            Pair<String, View.OnClickListener> item = (Pair<String, View.OnClickListener>) getItem(position);
            holder.itemView.setOnClickListener((View.OnClickListener) pair.second);
            if (item.second == null) {
                holder.itemView.setFocusable(false);
                holder.itemView.setClickable(false);
                holder.itemView.setPadding(16, 8, 16, 8);
            } else {
                holder.itemView.setFocusable(true);
                holder.itemView.setClickable(true);
                holder.itemView.setPadding(30, 30, 30, 30);
            }
        }

        private Object getItem(int position) {
            return mPairs.get(position);
        }

        @Override
        public int getItemCount() {
            return mPairs.size();
        }
    }

    static class MyHolder extends androidx.recyclerview.widget.RecyclerView.ViewHolder {
        public MyHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "onRequestPermissionsResult: 已授予权限");
                    new Thread(() -> {
                        MusicLibrary.initSongList(this);
                    }).start();
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onRequestPermissionsResult: 禁止权限");
                }
                break;
            default:
        }
    }

}
